<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>权限管理
            <!--<small>检查项管理</small>-->
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <!--<el-breadcrumb-item>检查项管理</el-breadcrumb-item>-->
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="权限关键字/权限名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="keyword" label="权限关键字" align="center"></el-table-column>
                <el-table-column prop="name" label="权限名称" align="center"></el-table-column>
                <el-table-column prop="description" label="权限说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑权限项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword">
                                    <el-input v-model="formData.keyword"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<!--引入axios拦截器-->
<script src="../plugins/interceptor.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                keyword: [{required: true, message: '权限关键字为必填项', trigger: 'blur'}],
                name: [{required: true, message: '权限名称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage()
        },
        methods: {
            //编辑
            handleEdit(row) {
                /*
                * 步骤：
                * 1.在这个方法里是为了提交用户修改过后的数据
                * 2.首先进行表单的校验，步骤就和新增的校验一样
                * 3.校验完成之后发送异步请求到后台进行数据的修改
                *
                * */
                //1.校验表单
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //校验通过，发送异步请求到后台修改数据信息

                        axios.post("/authority/updataById.do",this.formData).then((result)=>{
                            //隐藏编辑表单
                            this.dialogFormVisible4Edit=false
                            console.log(result);
                        if(result.data.flag){
                            //说明修改成功弹出消息提示框
                            this.$message({
                                message: result.data.message,
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: result.data.message,
                                type: 'error'
                            });
                        }
                    }).finally(()=>{
                            //重新发送分页查询数据
                            this.findPage()
                        })

                    } else {
                        //表单校验失败
                        this.$message.error("表单数据校验失败");
                return false;
            }
            })
            },
            //添加
            handleAdd() {
                //校验表单
                //this.$refs["formData"].validate获取表单的数据
                this.$refs["dataAddForm"].validate((flag) => {
                    if (flag) {

                        console.log(this.formData);
                        //表单验证成功，发送异步请求到后台查询数据
                        axios.post("/authority/add.do",this.formData)
                            .then((response) => {
                            console.log(response);
                        if (response.data.flag) {
                            //隐藏新增窗口
                            this.dialogFormVisible = false
                            //刷新页面
                            this.findPage()
                        }
                        //给用户提示，当添加成功和失败切换不同的颜色弹框
                        this.$message({
                            message: response.data.message,
                            type: response.data.flag ? 'success' : 'error'
                        });
                    }).catch((error) => {
                            console.log("添加失败")
                        })
                    } else {
                        this.$message.error("表单数据验证失败")
                        return false
                    }


                })

            },
            //分页查询
            findPage() {
                var parm = {
                    currentPage: this.pagination.currentPage,//页码数
                    pageSize: this.pagination.pageSize,//每页的条数
                    queryString: this.pagination.queryString
                };
                //发送异步请求去后台查询数据
                axios.post("/authority/queryPage.do",parm).then((result) => {
                    console.log(result);
                this.dataList = result.data.rows
                this.pagination.total = result.data.total
            })

            },
            // 重置表单
            resetForm() {
                this.formData = {}
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm()
                this.dialogFormVisible = true
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                // alert(row)
                /**
                 * 步骤：
                 * 1.因为要编辑数据，所以肯定要数据的回显，发送异步请求，根据id去查询数据，
                 * 2.当查到数据之后这时候我们就要把隐藏的编辑表单给展示出来并且把查到的数据回显到表单中
                 * 3.当数据回显之后进行修改，修改完成后点击确定调用handleEdit方法
                 */
                axios.get("/authority/queryById.do?id=" + row.id).then((result) => {
                    console.log(result);
                if (result.data.flag) {
                    //说明根据id查询成功
                    //显示出隐藏的编辑表单
                    this.dialogFormVisible4Edit = true
                    //将查出的结果赋值给formData，用于将数据回显到编辑表单上
                    this.formData = result.data.data;
                } else {
                    this.$message.error("获取数据失败，请刷新当前页面");
                }
            });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //currenPage为当前点击的页码数，将这个页码数赋值给currentPage
                this.pagination.currentPage = currentPage;
                //调用分页查询的方法
                this.findPage()

            },
            // 删除
            handleDelete(row) {
                //  alert(row.id)
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //走这一步说明用户点击的是确定按钮，然后开始发送异步请求去删除数据，因为删除时只携带一个id过去所以使用get的请求方式
                    axios.get("/authority/deleteById.do?id=" + row.id).then((result) => {
                        console.log(result);
                if (!result.data.flag) {
                    //说明删除失败,给用户提示删除失败信息
                    this.$message({
                        message: result.data.message,
                        type: 'error'
                    });
                } else {
                    //说明删除数据成功，给用户提示信息，
                    this.$message({
                        message: result.data.message,
                        type: 'success'
                    });
                    //然后调用分页查询方法重新刷新页面
                    this.findPage()
                }
            })
            }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
            });
            }
        }
    });
</script>
</html>